package com.meitianhui.convenitenbuy.module.store.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.module.store.model.StoreDetailInfo;
import com.meitianhui.convenitenbuy.util.DrivingRouteOverlay;
import com.meitianhui.convenitenbuy.util.TipUtils;

/**
 * Created by amy on 2017/12/1.
 * 该类的业务代码只有几句，所以就没弄presenter去分离了
 */

 public  class StoreNavigationActivity extends BaseToolbarActivity implements BaiduMap.OnMapClickListener, OnGetRoutePlanResultListener {

    private MapView mMapView;
    private BaiduMap mBaiduMap;
    private RoutePlanSearch mPlanSearch;
    private StoreDetailInfo mStoreDetail;

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTbTitle("导航");
        initId();
        initData();
        isEnable(false);
    }

    private void initData() {
        mBaiduMap = mMapView.getMap();

        mBaiduMap.setOnMapClickListener(this);

        //初始化搜索模块
        mPlanSearch = RoutePlanSearch.newInstance();
        mPlanSearch.setOnGetRoutePlanResultListener(this);

        //初始化位置信息
        gainLocation();
    }

    private void gainLocation() {
        Intent intent = getIntent();
        mStoreDetail = (StoreDetailInfo) intent.getSerializableExtra("storeDetail");
        String storeLatitude = mStoreDetail.detail.getLatitude();
        String storeLongitude = mStoreDetail.detail.getLongitude();
        if (!TextUtils.isEmpty(storeLatitude) && !TextUtils.isEmpty(storeLongitude)) {

            Double Mylongitude = 0.0;
            Double Mylatitude = 0.0;
            try {
                Mylongitude = Double.parseDouble(InfoProvider.getLongitude(this));
                Mylatitude = Double.parseDouble(InfoProvider.getLatitude(this));
            } catch (Exception e) {
                e.printStackTrace();
            }
            PlanNode stNode = PlanNode.withLocation(new LatLng(Mylatitude, Mylongitude));
            LatLng latLng = new LatLng(Double.valueOf(storeLatitude), Double.valueOf(storeLongitude));
            PlanNode enNode = PlanNode.withLocation(latLng);
            mPlanSearch.drivingSearch((new DrivingRoutePlanOption())
                    .from(stNode).to(enNode));
        }

    }

    private void initId() {
        mMapView = getViewById(R.id.store_navigation_map);
    }

    @Override
    protected int setView() {
        return R.layout.activity_store_navigation;
    }

    @Override
    protected void onRefreshData() {

    }

    @Override
    public void onMapClick(LatLng latLng) {
         mBaiduMap.hideInfoWindow();
    }

    @Override
    public boolean onMapPoiClick(MapPoi mapPoi) {
        return false;
    }

    @Override
    public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {

    }

    @Override
    public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {

    }

    @Override
    public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {

    }

    @Override
    public void onGetDrivingRouteResult(DrivingRouteResult result) {
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            TipUtils.showToast(App.getInstance(),"抱歉，未找到结果",3);
            return;
        }
        if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
            // 起终点或途经点地址有岐义，通过以下接口获取建议查询信息
            return;
        }
        if (result.error == SearchResult.ERRORNO.NO_ERROR) {
            //定义自己的图标
            DrivingRouteOverlay overlay = new MyDrivingRouteOverlay(mBaiduMap);
            mBaiduMap.setOnMarkerClickListener(overlay);
            overlay.setData(result.getRouteLines().get(0));
            overlay.addToMap();
            overlay.zoomToSpan();
        }
    }

    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
    }

    @Override
    public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {

    }

    @Override
    public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {

    }

    // 定制RouteOverly
    private class MyDrivingRouteOverlay extends DrivingRouteOverlay {

        public MyDrivingRouteOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public BitmapDescriptor getStartMarker() {
            return BitmapDescriptorFactory.fromResource(R.mipmap.icon_st);

        }

        @Override
        public BitmapDescriptor getTerminalMarker() {

            return BitmapDescriptorFactory.fromResource(R.mipmap.icon_en);

        }
    }


    @Override
    protected void onDestroy() {
        mPlanSearch.destroy();
        mMapView.onDestroy();
        super.onDestroy();
    }
}
